window.noteType = {
    EM_NONE: 0, //无效
    EM_TOUCH: 1, //点击
    EM_LEFT: 2, //左
    EM_RIGHT: 3, //右
    EM_UP: 4, //上
    EM_DOWN: 5, //下
    EM_SLUR: 6, //连音
    EM_BOOM: 7, //炸弹
    EM_Fast: 8, //快速
    EM_Double: 9 //双击
};

window.noteData = function () {
    this.id = 1;
    this.time = 2;
    this.moveTime = 2;
    this.type = 1;
    this.birthY = 375;

    this.ext1 = null;

    this.ext2 = null;

    this.isWarn = false;

    this.ext1placeholder = "";

    this.ext2placeholder = "";

    this.reset = function () {
        this.id = 1;
        this.time = 2;
        this.moveTime = 2;
        this.type = 1;
        this.birthY = 375;

        this.ext1 = null;

        this.ext2 = null;

        this.isWarn = false;

        this.ext1placeholder = "";

        this.ext2placeholder = "";
    }

};

window.simpleNoteData = function () {

    this.id = 1;
    this.time = 2;
    this.moveTime = 2;
    this.type = 1;
    this.birthY = 375;

    this.ext1 = null;

    this.ext2 = null;

};

window.sortNotes = function (items) {

    for (var i in items) {
        var item = items[i];

        item.time = parseFloat(item.time);
    }

    items.sort(function (a, b) {
        return a.time - b.time;
    });

    var index = 1;

    for (var i in items) {
        var item = items[i];
        item.id = index;
        index++;
    }

}

window.sortfunc = function (itemA, itemB) {
    return itemA.time < itemB.time;
}

window.onload = function () {
    var demo = new Vue({
        el: '#note',
        data: {
            message: 'note config!',
            items: [],
            options: [{
                    text: '单击',
                    value: 1
                },
                {
                    text: '左',
                    value: 2
                },
                {
                    text: '右',
                    value: 3
                },
                {
                    text: '上',
                    value: 4
                },
                {
                    text: '下',
                    value: 5
                },
                {
                    text: '连音',
                    value: 6
                },
                {
                    text: '炸弹',
                    value: 7
                },
                {
                    text: '快速',
                    value: 8
                },
                {
                    text: '双击',
                    value: 9
                }
            ],
            config: {
                music: "",
                delay: 0,
                list: [],
                version: 1,
            },
            addItem: new noteData()

        },
        watch: {
            items: function (val) {

                // console.log(val);

            },

        },
        methods: {
            save: function (event) {
                // this.message = item;

                console.log(this.items);

                var items = [];

                for (var i in this.items) {

                    var temp = this.items[i];

                    var data = new simpleNoteData();
                    data.id = parseInt(temp.id);
                    data.time = parseFloat(temp.time);
                    data.moveTime = parseFloat(temp.moveTime);
                    data.birthY = parseInt(temp.birthY);
                    data.type = parseInt(temp.type);

                    if (data.ext1 == null) {
                        data.ext1 = 0;
                    } else {
                        data.ext1 = parseFloat(temp.ext1);
                    }

                    if (data.ext2 == null) {
                        delete data.ext2;
                    } else {
                        data.ext2 = parseFloat(temp.ext2);
                    }

                    items.push(data);

                }

                this.config.list = items;

                if (this.config.version) {
                    this.config.version += 1;
                } else {
                    this.config.version = 1;
                }

                var str = JSON.stringify(this.config);

                var blob = new Blob([str], {
                    type: 'application/json'
                });
                saveAs(blob, "file.json"); //saveAs(blob,filename)
            },
            onFileChange: function (event) {
                var files = event.target.files || event.dataTransfer.files;

                if (!files.length)
                    return;

                var file = files[0];

                var fr = new FileReader();

                var self = this;

                fr.onload = function () {
                    console.log(this.result)

                    var json = JSON.parse(this.result);

                    self.config = json;

                    self.items = [];

                    for (var i in json.list) {
                        var data = json.list[i];

                        var note = new noteData();

                        for (var key in data) {
                            note[key] = data[key];
                        }

                        self.items.push(note);
                    }



                    console.log(self.items);

                };
                fr.readAsText(file);
                //this.createImage(files[0]);
            },

            onSelectedType(event, item) {
                item.ext1 = null;

                if (item.type == noteType.EM_SLUR) {
                    item.ext1placeholder = "连音结束时间";

                } else if (item.type == noteType.EM_Fast) {
                    item.ext1placeholder = "连击结束时间";
                    item.ext2placeholder = "连击次数";
                } else {
                    item.ext1placeholder = "";
                }

                // this.calculateMoney(item)
            },

            delNote: function (event, item) {

                if (item) {
                    console.log(item);
                    var index = this.items.indexOf(item);

                    if (index != -1) {
                        this.items.splice(index, 1);
                    }
                }

            },

            addNote: function (event) {

                this.items.push(this.addItem);

                this.addItem = new noteData();
                // this.addItem.reset();

                sortNotes(this.items);


                // var temp = document.getElementsByClassName("st-content-inner")[0];
                // temp.scrollTop = temp.scrollHeight;

            }

        }
    });

}